home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dflat2.zip / CONSOLE.C < prev    next >
Text File  |  1991-02-18  |  4KB  |  173 lines

  1. /* ----------- console.c ---------- */
  2.  
  3. #include <conio.h>
  4. #include <bios.h>
  5. #include <dos.h>
  6. #include "system.h"
  7. #include "keys.h"
  8.  
  9. /* ----- table of alt keys for finding shortcut keys ----- */
  10. char altconvert[] = {
  11.     ALT_A,ALT_B,ALT_C,ALT_D,ALT_E,ALT_F,ALT_G,ALT_H,
  12.     ALT_I,ALT_J,ALT_K,ALT_L,ALT_M,ALT_N,ALT_O,ALT_P,
  13.     ALT_Q,ALT_R,ALT_S,ALT_T,ALT_U,ALT_V,ALT_W,ALT_X,
  14.     ALT_Y,ALT_Z,ALT_0,ALT_1,ALT_2,ALT_3,ALT_4,ALT_5,
  15.     ALT_6,ALT_7,ALT_8,ALT_9,0
  16. };
  17.  
  18. unsigned video_mode;
  19. unsigned video_page;
  20.  
  21. static int near cursorpos[MAXSAVES];
  22. static int near cursorshape[MAXSAVES];
  23. static int cs = 0;
  24.  
  25. static union REGS regs;
  26.  
  27. #ifndef MSC
  28. #define ZEROFLAG 0x40
  29. /* ---- Test for keystroke ---- */
  30. int keyhit(void)
  31. {
  32.     _AH = 1;
  33.     geninterrupt(KEYBRD);
  34.     return (_FLAGS & ZEROFLAG) == 0;
  35. }
  36. #endif
  37.  
  38. /* ---- Read a keystroke ---- */
  39. int getkey(void)
  40. {
  41.     int c;
  42.     while (keyhit() == 0)
  43.         ;
  44.     if (((c = bioskey(0)) & 0xff) == 0)
  45.         c = (c >> 8) | 0x80;
  46.     return c & 0xff;
  47. }
  48.  
  49. /* ---------- read the keyboard shift status --------- */
  50. int getshift(void)
  51. {
  52.     regs.h.ah = 2;
  53.     int86(KEYBRD, ®s, ®s);
  54.     return regs.h.al;
  55. }
  56.  
  57. /* ------- macro to wait one clock tick -------- */
  58. #define wait()                     \
  59. {                                  \
  60.     int now = peek(0x40,0x6c);     \
  61.     while (now == peek(0x40,0x6c)) \
  62.         ;                          \
  63. }
  64.  
  65. /* -------- sound a buzz tone ---------- */
  66. void beep(void)
  67. {
  68.     wait();
  69.     outp(0x43, 0xb6);               /* program the frequency */
  70.     outp(0x42, (int) (COUNT % 256));
  71.     outp(0x42, (int) (COUNT / 256));
  72.     outp(0x61, inp(0x61) | 3);      /* start the sound */
  73.     wait();
  74.     outp(0x61, inp(0x61) & ~3);     /* stop the sound  */
  75. }
  76.  
  77. /* -------- get the video mode and page from BIOS -------- */
  78. void videomode(void)
  79. {
  80.     regs.h.ah = 15;
  81.     int86(VIDEO, ®s, ®s);
  82.     video_mode = regs.h.al;
  83.     video_page = regs.x.bx;
  84.     video_page &= 0xff00;
  85.     video_mode &= 0x7f;
  86. }
  87.  
  88. /* ------ position the cursor ------ */
  89. void cursor(int x, int y)
  90. {
  91.     videomode();
  92.     regs.x.dx = ((y << 8) & 0xff00) + x;
  93.     regs.x.ax = 0x0200;
  94.     regs.x.bx = video_page;
  95.     int86(VIDEO, ®s, ®s);
  96. }
  97.  
  98. /* ------ get cursor shape and position ------ */
  99. static void near getcursor(void)
  100. {
  101.     videomode();
  102.     regs.h.ah = READCURSOR;
  103.     regs.x.bx = video_page;
  104.     int86(VIDEO, ®s, ®s);
  105. }
  106.  
  107. /* ------- get the current cursor position ------- */
  108. void curr_cursor(int *x, int *y)
  109. {
  110.     getcursor();
  111.     *x = regs.h.dl;
  112.     *y = regs.h.dh;
  113. }
  114.  
  115. /* ------ save the current cursor configuration ------ */
  116. void savecursor(void)
  117. {
  118.     if (cs < MAXSAVES)    {
  119.         getcursor();
  120.         cursorshape[cs] = regs.x.cx;
  121.         cursorpos[cs] = regs.x.dx;
  122.         cs++;
  123.     }
  124. }
  125.  
  126. /* ---- restore the saved cursor configuration ---- */
  127. void restorecursor(void)
  128. {
  129.     if (cs)    {
  130.         --cs;
  131.         videomode();
  132.         regs.x.dx = cursorpos[cs];
  133.         regs.h.ah = SETCURSOR;
  134.          regs.x.bx = video_page;
  135.         int86(VIDEO, ®s, ®s);
  136.         set_cursor_type(cursorshape[cs]);
  137.     }
  138. }
  139.  
  140. /* ------ make a normal cursor ------ */
  141. void normalcursor(void)
  142. {
  143.     set_cursor_type(0x0607);
  144. }
  145.  
  146. /* ------ hide the cursor ------ */
  147. void hidecursor(void)
  148. {
  149.     getcursor();
  150.     regs.h.ch |= HIDECURSOR;
  151.     regs.h.ah = SETCURSORTYPE;
  152.     int86(VIDEO, ®s, ®s);
  153. }
  154.  
  155. /* ------ unhide the cursor ------ */
  156. void unhidecursor(void)
  157. {
  158.     getcursor();
  159.     regs.h.ch &= ~HIDECURSOR;
  160.     regs.h.ah = SETCURSORTYPE;
  161.     int86(VIDEO, ®s, ®s);
  162. }
  163.  
  164. /* ---- use BIOS to set the cursor type ---- */
  165. void set_cursor_type(unsigned t)
  166. {
  167.     videomode();
  168.     regs.h.ah = SETCURSORTYPE;
  169.      regs.x.bx = video_page;
  170.     regs.x.cx = t;
  171.     int86(VIDEO, ®s, ®s);
  172. }
  173.